using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Net;
using FIRADriverLibrary;
using RoBOSSCommunicator;

namespace PotentialFieldDriver
{


	public class CreateFiraDriverForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox robossGroupBox;
		private System.Windows.Forms.RadioButton robossRadioButton;
		private System.Windows.Forms.GroupBox cameraGroupBox;
		private System.Windows.Forms.RadioButton cameraRadioButton;
		private System.Windows.Forms.Label filenameLabel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button connectRoBOSSButton;
		private System.Windows.Forms.Button connectCameraButton;


		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox ipTextBox;
		private System.Windows.Forms.TextBox portTextBox;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Panel panel4;
		private System.Windows.Forms.Panel panel5;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Panel panel6;
		private System.Windows.Forms.Panel panel7;
		private System.Windows.Forms.Panel panel8;
		private System.Windows.Forms.Panel panel9;
		private System.Windows.Forms.Panel panel10;
		private System.Windows.Forms.TextBox goalWidthTextBox;
		private System.Windows.Forms.TextBox areaHeightTextBox;
		private System.Windows.Forms.TextBox areaWidthTextBox;
		private System.Windows.Forms.TextBox areaMinYTextBox;
		private System.Windows.Forms.TextBox areaMinXTextBox;


		private FIRADriver firaDriver;
        private Communicator communicator;
		public FIRADriver FiraDriver
		{
			get { return firaDriver; }
		}

        public Communicator Communicator
        {
            get { return communicator; }
        }

		float areaMinX = 0.15f;
		float areaMinY = 0.0f;
		float areaWidth = 2.2f;
		float areaHeight = 1.8f;

	
		public RectangleF PithArea 
		{
			get
			{
				return new RectangleF(areaMinX,areaMinY,areaWidth,areaHeight);
			}
		}

		float goalWidth = 0.4f;
		public float GoalWidth 
		{
			get
			{
			return goalWidth;
			}
		}


		public CreateFiraDriverForm()
		{
			InitializeComponent();
			try
			{
				ipTextBox.Text = Dns.Resolve(Dns.GetHostName()).AddressList[0].ToString();
			}
			catch {}
		}


		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.robossGroupBox = new System.Windows.Forms.GroupBox();
			this.connectRoBOSSButton = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.ipTextBox = new System.Windows.Forms.TextBox();
			this.portTextBox = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.robossRadioButton = new System.Windows.Forms.RadioButton();
			this.cameraGroupBox = new System.Windows.Forms.GroupBox();
			this.filenameLabel = new System.Windows.Forms.Label();
			this.connectCameraButton = new System.Windows.Forms.Button();
			this.cameraRadioButton = new System.Windows.Forms.RadioButton();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.panel10 = new System.Windows.Forms.Panel();
			this.panel9 = new System.Windows.Forms.Panel();
			this.panel8 = new System.Windows.Forms.Panel();
			this.panel7 = new System.Windows.Forms.Panel();
			this.panel6 = new System.Windows.Forms.Panel();
			this.label7 = new System.Windows.Forms.Label();
			this.goalWidthTextBox = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.areaHeightTextBox = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.areaWidthTextBox = new System.Windows.Forms.TextBox();
			this.panel5 = new System.Windows.Forms.Panel();
			this.panel4 = new System.Windows.Forms.Panel();
			this.label4 = new System.Windows.Forms.Label();
			this.areaMinYTextBox = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.areaMinXTextBox = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel3 = new System.Windows.Forms.Panel();
			this.panel2 = new System.Windows.Forms.Panel();
			this.robossGroupBox.SuspendLayout();
			this.cameraGroupBox.SuspendLayout();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// robossGroupBox
			// 
			this.robossGroupBox.Controls.Add(this.connectRoBOSSButton);
			this.robossGroupBox.Controls.Add(this.label1);
			this.robossGroupBox.Controls.Add(this.ipTextBox);
			this.robossGroupBox.Controls.Add(this.portTextBox);
			this.robossGroupBox.Controls.Add(this.label2);
			this.robossGroupBox.Location = new System.Drawing.Point(8, 8);
			this.robossGroupBox.Name = "robossGroupBox";
			this.robossGroupBox.Size = new System.Drawing.Size(352, 72);
			this.robossGroupBox.TabIndex = 0;
			this.robossGroupBox.TabStop = false;
			// 
			// connectRoBOSSButton
			// 
			this.connectRoBOSSButton.Location = new System.Drawing.Point(272, 40);
			this.connectRoBOSSButton.Name = "connectRoBOSSButton";
			this.connectRoBOSSButton.TabIndex = 3;
			this.connectRoBOSSButton.Text = "connect";
			this.connectRoBOSSButton.Click += new System.EventHandler(this.connectRoBOSSButton_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(24, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "ip";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// ipTextBox
			// 
			this.ipTextBox.Location = new System.Drawing.Point(32, 32);
			this.ipTextBox.Name = "ipTextBox";
			this.ipTextBox.Size = new System.Drawing.Size(120, 20);
			this.ipTextBox.TabIndex = 2;
			this.ipTextBox.Text = "controller ip address";
			// 
			// portTextBox
			// 
			this.portTextBox.Location = new System.Drawing.Point(200, 32);
			this.portTextBox.Name = "portTextBox";
			this.portTextBox.Size = new System.Drawing.Size(48, 20);
			this.portTextBox.TabIndex = 2;
			this.portTextBox.Text = "4468";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(168, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(24, 24);
			this.label2.TabIndex = 0;
			this.label2.Text = "port";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// robossRadioButton
			// 
			this.robossRadioButton.Checked = true;
			this.robossRadioButton.Location = new System.Drawing.Point(16, 8);
			this.robossRadioButton.Name = "robossRadioButton";
			this.robossRadioButton.Size = new System.Drawing.Size(128, 16);
			this.robossRadioButton.TabIndex = 1;
			this.robossRadioButton.TabStop = true;
			this.robossRadioButton.Text = "Connect to RoBOSS";
			this.robossRadioButton.CheckedChanged += new System.EventHandler(this.robossRadioButton_CheckedChanged);
			// 
			// cameraGroupBox
			// 
			this.cameraGroupBox.Controls.Add(this.filenameLabel);
			this.cameraGroupBox.Controls.Add(this.connectCameraButton);
			this.cameraGroupBox.Enabled = false;
			this.cameraGroupBox.Location = new System.Drawing.Point(8, 88);
			this.cameraGroupBox.Name = "cameraGroupBox";
			this.cameraGroupBox.Size = new System.Drawing.Size(352, 72);
			this.cameraGroupBox.TabIndex = 0;
			this.cameraGroupBox.TabStop = false;
			// 
			// filenameLabel
			// 
			this.filenameLabel.Location = new System.Drawing.Point(8, 24);
			this.filenameLabel.Name = "filenameLabel";
			this.filenameLabel.Size = new System.Drawing.Size(264, 40);
			this.filenameLabel.TabIndex = 0;
			this.filenameLabel.Text = "select configuration file ...";
			this.filenameLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.filenameLabel.Click += new System.EventHandler(this.filenameLabel_Click);
			// 
			// connectCameraButton
			// 
			this.connectCameraButton.Location = new System.Drawing.Point(272, 40);
			this.connectCameraButton.Name = "connectCameraButton";
			this.connectCameraButton.TabIndex = 3;
			this.connectCameraButton.Text = "connect";
			this.connectCameraButton.Click += new System.EventHandler(this.connectCameraButton_Click);
			// 
			// cameraRadioButton
			// 
			this.cameraRadioButton.Location = new System.Drawing.Point(16, 88);
			this.cameraRadioButton.Name = "cameraRadioButton";
			this.cameraRadioButton.Size = new System.Drawing.Size(120, 16);
			this.cameraRadioButton.TabIndex = 1;
			this.cameraRadioButton.Text = "Connect to Camera";
			this.cameraRadioButton.CheckedChanged += new System.EventHandler(this.robossRadioButton_CheckedChanged);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.panel10);
			this.groupBox1.Controls.Add(this.panel9);
			this.groupBox1.Controls.Add(this.panel8);
			this.groupBox1.Controls.Add(this.panel7);
			this.groupBox1.Controls.Add(this.panel6);
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Controls.Add(this.goalWidthTextBox);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.areaHeightTextBox);
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.areaWidthTextBox);
			this.groupBox1.Controls.Add(this.panel5);
			this.groupBox1.Controls.Add(this.panel4);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.areaMinYTextBox);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.areaMinXTextBox);
			this.groupBox1.Controls.Add(this.panel1);
			this.groupBox1.Controls.Add(this.panel3);
			this.groupBox1.Controls.Add(this.panel2);
			this.groupBox1.Location = new System.Drawing.Point(8, 168);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(352, 224);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Pitch dimensions";
			// 
			// panel10
			// 
			this.panel10.BackColor = System.Drawing.Color.Black;
			this.panel10.Location = new System.Drawing.Point(48, 64);
			this.panel10.Name = "panel10";
			this.panel10.Size = new System.Drawing.Size(16, 3);
			this.panel10.TabIndex = 20;
			// 
			// panel9
			// 
			this.panel9.BackColor = System.Drawing.Color.Black;
			this.panel9.Location = new System.Drawing.Point(48, 184);
			this.panel9.Name = "panel9";
			this.panel9.Size = new System.Drawing.Size(16, 3);
			this.panel9.TabIndex = 19;
			// 
			// panel8
			// 
			this.panel8.BackColor = System.Drawing.Color.Black;
			this.panel8.Location = new System.Drawing.Point(96, 192);
			this.panel8.Name = "panel8";
			this.panel8.Size = new System.Drawing.Size(2, 16);
			this.panel8.TabIndex = 18;
			// 
			// panel7
			// 
			this.panel7.BackColor = System.Drawing.Color.Black;
			this.panel7.Location = new System.Drawing.Point(280, 152);
			this.panel7.Name = "panel7";
			this.panel7.Size = new System.Drawing.Size(16, 3);
			this.panel7.TabIndex = 17;
			// 
			// panel6
			// 
			this.panel6.BackColor = System.Drawing.Color.Black;
			this.panel6.Location = new System.Drawing.Point(280, 96);
			this.panel6.Name = "panel6";
			this.panel6.Size = new System.Drawing.Size(16, 3);
			this.panel6.TabIndex = 16;
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(288, 104);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(40, 24);
			this.label7.TabIndex = 15;
			this.label7.Text = "goal width:";
			this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// goalWidthTextBox
			// 
			this.goalWidthTextBox.Location = new System.Drawing.Point(288, 128);
			this.goalWidthTextBox.Name = "goalWidthTextBox";
			this.goalWidthTextBox.Size = new System.Drawing.Size(40, 20);
			this.goalWidthTextBox.TabIndex = 14;
			this.goalWidthTextBox.Text = "0,4";
			this.goalWidthTextBox.TextChanged += new System.EventHandler(this.valueTextBox_TextChanged);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(128, 192);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(40, 24);
			this.label5.TabIndex = 13;
			this.label5.Text = "area width:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// areaHeightTextBox
			// 
			this.areaHeightTextBox.Location = new System.Drawing.Point(24, 120);
			this.areaHeightTextBox.Name = "areaHeightTextBox";
			this.areaHeightTextBox.Size = new System.Drawing.Size(40, 20);
			this.areaHeightTextBox.TabIndex = 12;
			this.areaHeightTextBox.Text = "1,8";
			this.areaHeightTextBox.TextChanged += new System.EventHandler(this.valueTextBox_TextChanged);
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(24, 88);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(40, 24);
			this.label6.TabIndex = 11;
			this.label6.Text = "area height:";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// areaWidthTextBox
			// 
			this.areaWidthTextBox.Location = new System.Drawing.Point(168, 192);
			this.areaWidthTextBox.Name = "areaWidthTextBox";
			this.areaWidthTextBox.Size = new System.Drawing.Size(40, 20);
			this.areaWidthTextBox.TabIndex = 10;
			this.areaWidthTextBox.Text = "2,2";
			this.areaWidthTextBox.TextChanged += new System.EventHandler(this.valueTextBox_TextChanged);
			// 
			// panel5
			// 
			this.panel5.BackColor = System.Drawing.Color.Black;
			this.panel5.Location = new System.Drawing.Point(248, 192);
			this.panel5.Name = "panel5";
			this.panel5.Size = new System.Drawing.Size(2, 16);
			this.panel5.TabIndex = 9;
			// 
			// panel4
			// 
			this.panel4.BackColor = System.Drawing.Color.Black;
			this.panel4.Location = new System.Drawing.Point(92, 60);
			this.panel4.Name = "panel4";
			this.panel4.Size = new System.Drawing.Size(8, 8);
			this.panel4.TabIndex = 8;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(104, 37);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(16, 24);
			this.label4.TabIndex = 7;
			this.label4.Text = "y:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// areaMinYTextBox
			// 
			this.areaMinYTextBox.Location = new System.Drawing.Point(120, 37);
			this.areaMinYTextBox.Name = "areaMinYTextBox";
			this.areaMinYTextBox.Size = new System.Drawing.Size(40, 20);
			this.areaMinYTextBox.TabIndex = 6;
			this.areaMinYTextBox.Text = "0,0";
			this.areaMinYTextBox.TextChanged += new System.EventHandler(this.valueTextBox_TextChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(104, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(16, 24);
			this.label3.TabIndex = 5;
			this.label3.Text = "x:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// areaMinXTextBox
			// 
			this.areaMinXTextBox.Location = new System.Drawing.Point(120, 16);
			this.areaMinXTextBox.Name = "areaMinXTextBox";
			this.areaMinXTextBox.Size = new System.Drawing.Size(40, 20);
			this.areaMinXTextBox.TabIndex = 4;
			this.areaMinXTextBox.Text = "0,15";
			this.areaMinXTextBox.TextChanged += new System.EventHandler(this.valueTextBox_TextChanged);
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.Gray;
			this.panel1.Location = new System.Drawing.Point(96, 64);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(152, 120);
			this.panel1.TabIndex = 1;
			// 
			// panel3
			// 
			this.panel3.BackColor = System.Drawing.Color.Gray;
			this.panel3.Location = new System.Drawing.Point(216, 96);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(56, 56);
			this.panel3.TabIndex = 3;
			// 
			// panel2
			// 
			this.panel2.BackColor = System.Drawing.Color.Gray;
			this.panel2.Location = new System.Drawing.Point(72, 96);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(56, 56);
			this.panel2.TabIndex = 2;
			// 
			// CreateFiraDriverForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(368, 399);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.robossRadioButton);
			this.Controls.Add(this.cameraRadioButton);
			this.Controls.Add(this.robossGroupBox);
			this.Controls.Add(this.cameraGroupBox);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "CreateFiraDriverForm";
			this.Text = "Create FiraDriver ";
			this.robossGroupBox.ResumeLayout(false);
			this.cameraGroupBox.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void robossRadioButton_CheckedChanged(object sender, System.EventArgs e)
		{
			if (robossRadioButton.Checked)
			{
				robossGroupBox.Enabled = true;
				cameraGroupBox.Enabled = false;
			}
			else
			{
				robossGroupBox.Enabled = false;
				cameraGroupBox.Enabled = true;
			}
		}

		private void filenameLabel_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog fileDialog = new OpenFileDialog();
			fileDialog.Title = "Load camera settings";
			fileDialog.Filter = "Camera Settings files (*.xml)|*.xml";
			if (fileDialog.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			filenameLabel.Text = fileDialog.FileName;

		}

		private void connectRoBOSSButton_Click(object sender, System.EventArgs e)
		{
            firaDriver = new FIRADriver();
            communicator = new Communicator();
			if (0 > firaDriver.ConnectToRoBOSS(ipTextBox.Text, portTextBox.Text))
			{
				MessageBox.Show("An error occured while connecting to RoBOSS:\n"+FIRADriver.lastError,"Failed to create FiraController!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
            if (0 > communicator.Connect(ipTextBox.Text, portTextBox.Text))
            {
                MessageBox.Show("An error occured while connecting to RoBOSS:\n" + Communicator.lastError, "Failed to create Communicator!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
			DialogResult = DialogResult.OK;
		}

		private void connectCameraButton_Click(object sender, System.EventArgs e)
		{
			if (!File.Exists(filenameLabel.Text))
			{ 
				MessageBox.Show("Plese, select a proper camera connection file","Failed to create FiraController!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				filenameLabel_Click(this,null);
				return;
			}
            firaDriver = new FIRADriver();
			if (0 > firaDriver.ConnectToCamera(filenameLabel.Text))
			{
				MessageBox.Show("An error occured while connecting to camera:\n"+FIRADriver.lastError,"Failed to create FiraController!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			DialogResult = DialogResult.OK;		
		}

		private void valueTextBox_TextChanged(object sender, System.EventArgs e)
		{
			try
			{				
				areaMinX = float.Parse(areaMinXTextBox.Text);
				areaMinY = float.Parse(areaMinYTextBox.Text);
				areaWidth = float.Parse(areaWidthTextBox.Text);
				areaHeight= float.Parse(areaHeightTextBox.Text);
				goalWidth = float.Parse(goalWidthTextBox.Text);
			}
			catch 
			{
				connectRoBOSSButton.Enabled = false;
				connectCameraButton.Enabled = false;
				areaMinXTextBox.BackColor = Color.LightSalmon;
				areaMinYTextBox.BackColor = Color.LightSalmon;
				areaWidthTextBox.BackColor = Color.LightSalmon;
				areaHeightTextBox.BackColor = Color.LightSalmon;
				goalWidthTextBox.BackColor = Color.LightSalmon;
				return;
			}
			connectRoBOSSButton.Enabled = true;
			connectCameraButton.Enabled = true;
			areaMinXTextBox.BackColor = SystemColors.Window;
			areaMinYTextBox.BackColor = SystemColors.Window;
			areaWidthTextBox.BackColor = SystemColors.Window;
			areaHeightTextBox.BackColor  = SystemColors.Window;
			goalWidthTextBox.BackColor = SystemColors.Window;		
		}

	}
}
